/**
 * Created by chenzheyu on 2017/4/24.
 */
$(function () {


    var SEOVal = '';
    var id = '';
    var pid = '';
    var limit = 20;

    $.ajax({
        url: ctx + "/rentnotice/selectAllCategoryTop",
        async:false,
        success: function (data) {
            var TopPid;
            var TopGenreOption = {
                data: [
                    {name: '全部', Val: ''}
                ],
                onChange: function (e) {
                    TopPid = e.val;
                    if (!TopPid == '') {
                        $.ajax({
                            url: ctx + "/rentnotice/selectCategoryByPid",
                            data: {
                                pid: TopPid
                            },
                            async:false,
                            success: function (data) {
                                    var GenreOption = {
                                    data: [
                                        {name: '全部', Val: ''}
                                    ],
                                    onChange: function (e) {
                                    }
                                };
                                for (var i = 0; i < data.length; i++) {
                                    (function () {
                                        GenreOption.data.push({name: data[i].categoryName, Val: data[i].id})
                                    })()
                                }
                                $('.SEO .Genre .Item').DownPullSelect(GenreOption);
                            }
                        });
                    } else {
                        var GenreOption = {
                            data: [
                                {name: '全部', Val: ''}
                            ],
                            onChange: function (e) {
                            }
                        };
                        $('.SEO .Genre .Item').DownPullSelect(GenreOption);
                    }
                }
            };
            for (var i = 0; i < data.length; i++) {
                (function () {
                    TopGenreOption.data.push({name: data[i].categoryName, Val: data[i].id})
                })()
            }
            $('.SEO .TopGenre .Item').DownPullSelect(TopGenreOption);


        }
    });

    function NoticeListOrder(page, SEO) {

        $.ajax({
            url: ctx + '/rentnotice/selectNoticesByTitle',
            async:false,
            data: {
                category: id,
                categoryTop: pid,
                title: SEO,
                "limit": limit,
                "pager.offset": (page - 1) * limit
            },
            type: 'GET',
            success: function (data) {
                var OrderOption = {
                    data: {
                        Thead: {
                            title: '标题',
                            sort: '排序',
                            classify: '所属子分类',
                            classifyTop: '所属主分类',
                            action: '操作'
                        },
                        Tbody: []
                    }
                };


                for (var i = 0; i < data.content.length; i++) {
                    (function () {
                        var index = i;
                        var Val = {
                            id: {id: data.content[index].id},
                            title: data.content[index].title,
                            sort: data.content[index].seq,
                            classify: data.content[index].categoryName,
                            classifyTop: data.content[index].categoryTopName,
                            action: 'wqe'
                        };
                        var Action = $('<div class="Action"></div>');
                        var redact = $('<span class="redact"></span>');
                        var deleteBut = $('<span class="delete"></span>');
                        redact.ActionButtons({
                            data: '编辑', onChange: function (e) {
                                $.ajax({
                                    url: ctx + "/rentnotice/selectNotice",
                                    data: {
                                        id: data.content[index].id
                                    },
                                    async:false,
                                    success: function (data) {
                                        var Data = data;
                                        var CalendarPadText = data.categoryTopName;
                                        var SubClassText = data.categoryName;
                                        var CalendarPadID = data.categoryTop;
                                        var SubClassID = data.category;
                                        var Titles = data.title;
                                        var ListContent = data.notice;
                                        var Sort = data.seq;
                                        var AddNoticeListOption = {
                                            data: {
                                                Title: '编辑内容',
                                                Content: '',
                                                OkChange: function (e, Remove) {

                                                    var CustomModalBox = e;
                                                    Titles = CustomModalBox.find('.Titles input').val();
                                                    ListContent = CustomModalBox.find('.textarea textarea').val();
                                                    Sort = CustomModalBox.find('.Sort input').val();
                                                    if (!CalendarPadText == '') {
                                                        if (!SubClassText == '') {
                                                            if (!Titles == '') {
                                                                if (!ListContent == '') {
                                                                    if (!Sort == '') {
                                                                        $.ajax({
                                                                            url: ctx + '/rentnotice/updateNotice',
                                                                            async:false,
                                                                            data: {
                                                                                id: data.id,
                                                                                title: Titles,
                                                                                seq: Sort,
                                                                                categoryName: SubClassText,
                                                                                categoryTopName: CalendarPadText,
                                                                                categoryTop: CalendarPadID,
                                                                                category: SubClassID,
                                                                                notice: ListContent
                                                                            },
                                                                            type: 'POST',
                                                                            success: function (data) {
                                                                                if (data.message == '修改成功') {
                                                                                    new $().Overlays({
                                                                                        data: {
                                                                                            text: '编辑成功！',
                                                                                            state: 'Ok'
                                                                                        }
                                                                                    });
                                                                                    Remove();
                                                                                    SEOVal = '';
                                                                                    NoticeListOrder(1, SEOVal);
                                                                                } else {
                                                                                    new $().Overlays({
                                                                                        data: {
                                                                                            text: '编辑失败，请稍后重试！',
                                                                                            state: 'No'
                                                                                        }
                                                                                    });
                                                                                }
                                                                            }
                                                                        })
                                                                    } else {
                                                                        new $().Overlays({
                                                                            data: {
                                                                                text: '请输入排序！',
                                                                                state: 'No'
                                                                            }
                                                                        });
                                                                    }
                                                                } else {
                                                                    new $().Overlays({
                                                                        data: {
                                                                            text: '请输入内容！',
                                                                            state: 'No'
                                                                        }
                                                                    });
                                                                }
                                                            } else {
                                                                new $().Overlays({data: {text: '请输标题！', state: 'No'}});
                                                            }
                                                        } else {
                                                            new $().Overlays({data: {text: '请选择子类型！', state: 'No'}});
                                                        }
                                                    } else {
                                                        new $().Overlays({data: {text: '请选择主类型！', state: 'No'}});
                                                    }


                                                }
                                            },
                                            OKHide: true
                                        };

                                        var AddNoticeListContent = $('<div class="AddNoticeListContent"></div>');
                                        AddNoticeListContent.html('<div class="rows CalendarPad"><div class="Name">主类：</div><div class="Options"></div></div><div class="rows SubClass"><div class="Name">子类：</div><div class="Options"></div></div><div class="rows Titles"><div class="Name">标题：</div><input type="text" placeholder="请输入标题" value="' + Titles + '"></div><div class="rows  textarea"><div class="Name">内容：</div><textarea placeholder="请输入内容">' + ListContent + '</textarea></div><div class="rows Sort"><div class="Name">排序：</div><input type="number" placeholder="请输入排序号" value="' + Sort + '"></div>');

                                        $.ajax({
                                            url: ctx + "/rentnotice/selectAllCategoryTop",
                                            async:false,
                                            success: function (data) {
                                                var TopPid;
                                                var TopGenreOption = {
                                                    data: [],
                                                    onChange: function (e) {
                                                        CalendarPadID = e.val;
                                                        CalendarPadText = e.text;
                                                        TopPid = e.val;
                                                        if (!TopPid == '') {
                                                            $.ajax({
                                                                url: ctx + "/rentnotice/selectCategoryByPid",
                                                                data: {
                                                                    pid: TopPid
                                                                },
                                                                async:false,
                                                                success: function (data) {
                                                                    var GenreOption = {
                                                                        data: [],
                                                                        onChange: function (e) {
                                                                            SubClassText = e.text;
                                                                            SubClassID = e.val;
                                                                        }
                                                                    };
                                                                    for (var i = 0; i < data.length; i++) {
                                                                        (function () {
                                                                            GenreOption.data.push({
                                                                                name: data[i].categoryName,
                                                                                Val: data[i].id
                                                                            })
                                                                        })()
                                                                    }
                                                                    AddNoticeListContent.find('.SubClass  .Options').DownPullSelect(GenreOption);
                                                                    AddNoticeListContent.find('.SubClass .Select').val(Data.category);
                                                                    AddNoticeListContent.find('.SubClass .Select').change();
                                                                }
                                                            });
                                                        } else {
                                                            var GenreOption = {
                                                                data: [
                                                                    {name: '该主类下没有子类', Val: ''}
                                                                ],
                                                                onChange: function (e) {
                                                                    SubClassText = e.text;
                                                                    SubClassID = e.val;
                                                                }
                                                            };
                                                            AddNoticeListContent.find('.SubClass  .Options').DownPullSelect(GenreOption);
                                                            AddNoticeListContent.find('.SubClass .Select').change();
                                                        }
                                                    }
                                                };
                                                for (var i = 0; i < data.length; i++) {
                                                    (function () {
                                                        TopGenreOption.data.push({
                                                            name: data[i].categoryName,
                                                            Val: data[i].id
                                                        })
                                                    })()
                                                }
                                                AddNoticeListContent.find('.CalendarPad  .Options').DownPullSelect(TopGenreOption);
                                                AddNoticeListContent.find('.CalendarPad .Select').val(Data.categoryTop);
                                                AddNoticeListContent.find('.CalendarPad .Select').change();
                                            }
                                        });


                                        AddNoticeListOption.data.Content = AddNoticeListContent;
                                        new $('body').ModalBox(AddNoticeListOption);


                                    }
                                })
                            }
                        });
                        var start=true;
                        var startBtion=true;
                        deleteBut.ActionButtons({
                            data: '删除', onChange: function () {

                                if(startBtion){
                                    startBtion=false;
                                    var deleteButOption={
                                        data: {
                                            Title: '删除提示',
                                            Content: '是否要删除该条内容',
                                            OkChange: function () {
                                                if(start){
                                                    start=false;
                                                    $.ajax({
                                                        url: ctx + '/rentnotice/deleteNotice',
                                                        data: {
                                                            id: data.content[index].id
                                                        },
                                                        async:false,
                                                        type: 'POST',
                                                        success: function (data) {
                                                            if (data.message == '删除成功') {
                                                                new $().Overlays({
                                                                    data: {
                                                                        text: '删除成功！',
                                                                        state: 'Ok'
                                                                    }
                                                                });
                                                                SEOVal = '';
                                                                NoticeListOrder(1, SEOVal);
                                                            } else {
                                                                new $().Overlays({data: {text: '删除失败，请稍后重试！', state: 'No'}});
                                                            }
                                                        }
                                                    })
                                                }
                                            },
                                            CancelChange:function () {
                                                startBtion=true
                                            }
                                        }
                                    };

                                    new $('body').ModalBox(deleteButOption)
                                }

                            }
                        });
                        Action.append(redact);
                        Action.append(deleteBut);

                        Val.action =Action;
                        OrderOption.data.Tbody.push(Val);
                    })()
                }

                var PagingMdOption = {
                    data: {
                        rows: 20,
                        nowrows: page,
                        total: data.totalElements,
                        change: function (index) {
                            NoticeListOrder(index, SEOVal);
                        }
                    }
                };


                    $('.OrderMid').find('.CustomOrder').remove();
                    $('.OrderMid').find('.PagingMdPagingMd').remove();
                    $('.OrderMid').Order(OrderOption);
                    $('.OrderMid').PagingMd(PagingMdOption);




            }
        })
    }

    NoticeListOrder(1, SEOVal);


    $('.SEOBut').ActionButtons({
        data: '查询', onChange: function () {
            pid = $('.TopGenre .Select').val();
            id = $('.Genre .Select').val();
            SEOVal = $('.GenreTileText').val();
            NoticeListOrder(1, SEOVal);
        }
    });

    $(".SEO  input").keyup(function (event) {
        switch (event.keyCode) {
            case 13:
                console.log(123)
                $(".SEOBut .CustomActionButtons").click();
                break;
        }
    });

    $('.AddNoticeList').ActionButtons({
        data: '添加内容', onChange: function () {
            var CalendarPadText;
            var SubClassText;
            var CalendarPadID;
            var SubClassID;
            var AddNoticeListOption = {
                data: {
                    Title: '添加内容',
                    Content: '',
                    OkChange: function (e, Remove) {

                        var CustomModalBox = e;
                        var Titles = CustomModalBox.find('.Titles input').val();
                        var ListContent = CustomModalBox.find('.textarea textarea').val();
                        var Sort = CustomModalBox.find('.Sort input').val();

                        if (!CalendarPadText == '') {
                            if (!SubClassText == '') {
                                if (!Titles == '') {
                                    if (!ListContent == '') {
                                        if (!Sort == '') {
                                            $.ajax({
                                                url: ctx + '/rentnotice/insertNotice',
                                                data: {
                                                    title: Titles,
                                                    seq: Sort,
                                                    categoryName: SubClassText,
                                                    categoryTopName: CalendarPadText,
                                                    categoryTop: CalendarPadID,
                                                    category: SubClassID,
                                                    notice: ListContent
                                                },
                                                async:false,
                                                type: 'POST',
                                                success: function (data) {
                                                    if (data.message == '创建成功') {
                                                        new $().Overlays({data: {text: '添加成功！', state: 'Ok'}});
                                                        Remove();
                                                        SEOVal = '';
                                                        NoticeListOrder(1, SEOVal);
                                                    } else {
                                                        new $().Overlays({data: {text: '创建失败，请稍后重试！', state: 'No'}});
                                                    }
                                                }
                                            })
                                        } else {
                                            new $().Overlays({data: {text: '请输入排序！', state: 'No'}});
                                        }
                                    } else {
                                        new $().Overlays({data: {text: '请输入内容！', state: 'No'}});
                                    }
                                } else {
                                    new $().Overlays({data: {text: '请输标题！', state: 'No'}});
                                }
                            } else {
                                new $().Overlays({data: {text: '请选择类型！', state: 'No'}});
                            }
                        } else {
                            new $().Overlays({data: {text: '请选择类型！', state: 'No'}});
                        }


                    }
                },
                OKHide: true
            };

            var AddNoticeListContent = $('<div class="AddNoticeListContent"></div>');
            AddNoticeListContent.html('<div class="rows CalendarPad"><div class="Name">主类：</div><div class="Options"></div></div><div class="rows SubClass"><div class="Name">子类：</div><div class="Options"></div></div><div class="rows Titles"><div class="Name">标题：</div><input type="text" placeholder="请输入标题"></div><div class="rows  textarea"><div class="Name">内容：</div><textarea placeholder="请输入内容"></textarea></div><div class="rows Sort"><div class="Name">排序：</div><input type="number" placeholder="请输入排序号"></div>');

            $.ajax({
                url: ctx + "/rentnotice/selectAllCategoryTop",
                async:false,
                success: function (data) {
                    var TopPid;
                    var TopGenreOption = {
                        data: [],
                        onChange: function (e) {
                            CalendarPadID = e.val;
                            CalendarPadText = e.text;
                            TopPid = e.val;
                            if (!TopPid == '') {
                                $.ajax({
                                    url: ctx + "/rentnotice/selectCategoryByPid",
                                    data: {
                                        pid: TopPid
                                    },
                                    async:false,
                                    success: function (data) {
                                        var GenreOption = {
                                            data: [],
                                            onChange: function (e) {
                                                SubClassText = e.text;
                                                SubClassID = e.val;
                                            }
                                        };
                                        if (data.length > 0) {
                                            for (var i = 0; i < data.length; i++) {
                                                (function () {
                                                    var index=i;
                                                    GenreOption.data.push({
                                                        name: data[index].categoryName,
                                                        Val: data[index].id
                                                    })
                                                })()
                                            }
                                        } else {
                                            GenreOption.data.push({name: '该主类里没有子类', Val: ''})
                                        }

                                        AddNoticeListContent.find('.SubClass  .Options').DownPullSelect(GenreOption);
                                    }
                                });
                            } else {
                                var GenreOption = {
                                    data: [],
                                    onChange: function (e) {
                                        SubClassText = e.text;
                                        SubClassID = e.val;
                                    }
                                };
                                AddNoticeListContent.find('.SubClass  .Options').DownPullSelect(GenreOption);
                            }
                        }
                    };
                    for (var i = 0; i < data.length; i++) {
                        (function () {
                            var index=i;
                            TopGenreOption.data.push({name: data[index].categoryName, Val: data[index].id})
                        })()
                    }
                    AddNoticeListContent.find('.CalendarPad  .Options').DownPullSelect(TopGenreOption);


                }
            });

            AddNoticeListOption.data.Content = AddNoticeListContent;
            new $('body').ModalBox(AddNoticeListOption)
        }
    })
});